const cheerio = require('cheerio');
const request = require('request');
const path = require('path');
const fs = require('fs');

const url = 'https://www.nanshig.com/shop/cate-258-0-0-0-0-0-0-0-0.html';

const goodlist =[];


request(url,(err,res,body)=>{
    // JQ化
    let $ = cheerio.load(body);

    // 同步创建文件
    fs.mkdirSync('./img');

    // 遍历
    $('.list_pic').find('.item').each((idx,item)=>{

        let $el = $(item);

        // 名字、原价、现价、销量、评论、店铺
        let name = $el.find('.goods-name a').text();
        let old_price = $el.find('.goods-price .market-price').text().match(/[\d\.]+/)[0];
        let sale_price = $el.find('.goods-price .sale-price').text().match(/[\d\.]+/)[0];
        let sale = $el.find('.sell-stat ul li:first-child a').text();
        let comment = $el.find('.sell-stat ul li:nth-child(2) a').text();
        let store = $el.find('.store .name').text();
        let imgurl = $el.find('.goods-pic img').attr('data-url');

        // 1.数据重构
        const good = {
            name,
            imgurl,
            old_price,
            sale_price,
            sale,
            comment,
            store
        }

        // 插入空数组
        goodlist.push(good);

        // 2.爬取图片
        // 2.1 获取文件名
        const fileName = path.basename(imgurl);
        // 2.2 创建写入流
        const picStream = fs.createWriteStream('./img/'+fileName);
        // 2.3 请求的图片数据以流的形式写入指定位置
        request(imgurl).pipe(picStream);

    })

    // 3.写入流：数据写入json文件
    // 3.1 创建写入流
    const jsonStream = fs.createWriteStream('./data.json');
    // 3.2 使用utf8编码，并把数据以字符串形式写入数据
    jsonStream.write(JSON.stringify(goodlist),'UTF8');
    // 3.3 提示
    jsonStream.on('finish',()=>{
        console.log('写入完成');
    })
    // 3.4 标记文本末尾
    jsonStream.end()
})